<?php
session_start();

include('db.inc.php');
include('cfg.inc.php');
include('user.class.php');
$continue=false;
if(isset($_SESSION['user']) && isset($_SESSION['clave'])) {
    $continue=true;
    $db=new DbManager();
    $db->connect(DBHOST, DBUSER, DBPWD,true,true,DBNAME,"");
    if($continue) {
        $us=$_SESSION['user'];
        $cl=$_SESSION['clave'];
        $en=$_SESSION['entidad'];

        validaUsuario($us,$cl,$en,$db);
    }else {
        $list=$db->query_select("cllg_entity",NULL);
    }    
}

function validaUsuario($usuario,$clave,$entidad,$db) {
    $r=$db->query_select("cllg_user",sprintf("USER_NAME like '%s'",$usuario));
	//echo $db->lastQuery;
    if(count($r)>0) {
        $u=$r[0];
        $pass=$u["PASSWORD"];
        $sysUser=new SysUser();
		$sysUser->name=$u['USER_NAME'];
		$sysUser->timeLogon=time();
		$sysUser->lg("ACCESO A APLICACION",NULL);
		$sysUser->entity=$entidad;

		//$sysUser=getCicloEntidad($db,$sysUser);
		

		$_SESSION['user']=serialize($sysUser);
		session_cache_expire(100000);
    }
	header('Location: ../main.php');
}

function getCicloEntidad($db,$user) {
    $rs=$db->query_select("cllg_college_cycle","STATUS=1 AND FK_ENTITY=".$user->entity);
    if(count($rs)>0) {
        $rs=$rs[0];
        $user->cycle=$rs["CODE"];
    }else {
        $user->cycle=null;
    }
    return $user;
}

?>